package com.yanqu.road.server.gameplayer.module.activity.patronsfund;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.festivalgift.FestivalGiftData;
import com.yanqu.road.entity.activity.patronsfund.PatronsFundBuyData;
import com.yanqu.road.entity.activity.patronsfund.PatronsFundConditionCom;
import com.yanqu.road.entity.activity.patronsfund.PatronsFundUser;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.bussiness.activity.FestivalGiftBusiness;
import com.yanqu.road.logic.bussiness.activity.PatronsFundBusiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.patronsfund.PatronsFundMgr;
import org.apache.mina.util.ConcurrentHashSet;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class PatronsFundModule extends GeneralModule {

    //玩家基金信息,K:活动ID,V:门客map
    private Map<Integer, Map<Integer, PatronsFundUser>> patronsFundUserMap = new ConcurrentHashMap<>();

    //购买基金记录.K:门客ID,V:档位MAP
    private Map<Integer, Map<Integer, PatronsFundBuyData>> patronsFundBuyDataMap = new ConcurrentHashMap<>();

    //达成日志,K:活动ID,V:任务map
    private Map<Integer, Map<Integer, PatronsFundConditionCom>> patronsFundConditionComMap = new ConcurrentHashMap<>();

    public PatronsFundModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        List<ActivityInfo> activityList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.PatronsFund.getValue());
        if (!activityList.isEmpty()) {
            //购买基金记录
            this.patronsFundBuyDataMap = PatronsFundBusiness.getPatronsFundBuyData(player.getUserId());
            for (ActivityInfo activityInfo : activityList) {
                //玩家基金信息
                Map<Integer, PatronsFundUser> userMap = PatronsFundBusiness.getPatronsFundBuyData(player.getUserId(), activityInfo.getActivityId());
                this.patronsFundUserMap.put(activityInfo.getActivityId(), userMap);
                //达成日志
                Map<Integer, PatronsFundConditionCom> conditionComMap = PatronsFundBusiness.getPatronsFundConditionCom(player.getUserId(), activityInfo.getActivityId());
                this.patronsFundConditionComMap.put(activityInfo.getActivityId(), conditionComMap);
            }
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        for (Map<Integer, PatronsFundBuyData> mallMap : this.patronsFundBuyDataMap.values()) {
            for (PatronsFundBuyData patronsFundBuyData : mallMap.values()) {
                if (patronsFundBuyData.isInsertOption()) {
                    PatronsFundBusiness.addPatronsFundBuyData(patronsFundBuyData);
                } else if (patronsFundBuyData.isUpdateOption()) {
                    PatronsFundBusiness.updatePatronsFundBuyData(patronsFundBuyData);
                }
            }
        }
        for (Map<Integer, PatronsFundUser> patronsMap : this.patronsFundUserMap.values()) {
            for (PatronsFundUser patronsFundUser : patronsMap.values()) {
                if (patronsFundUser.isInsertOption()) {
                    PatronsFundBusiness.addPatronsFundUser(patronsFundUser);
                } else if (patronsFundUser.isUpdateOption()) {
                    PatronsFundBusiness.updatePatronsFundUser(patronsFundUser);
                }
            }
        }
        for (Map<Integer, PatronsFundConditionCom> conditionComMap : this.patronsFundConditionComMap.values()) {
            for (PatronsFundConditionCom patronsFundConditionCom : conditionComMap.values()) {
                if (patronsFundConditionCom.isInsertOption()) {
                    PatronsFundBusiness.addPatronsFundConditionCom(patronsFundConditionCom);
                } else if (patronsFundConditionCom.isUpdateOption()) {
                    PatronsFundBusiness.updatePatronsFundConditionCom(patronsFundConditionCom);
                }
            }
        }
        return true;
    }

    /**
     * 资质变化
     * @param patronsList
     */
    public void qualificationChange(List<UserPatrons> patronsList) {
        for (UserPatrons userPatrons : patronsList) {
            if (PatronsFundMgr.getPartnerIds().contains(userPatrons.getPatronsId())) {
                player.notifyListener(eGamePlayerEventType.PatronsFundIntelligence.getValue(), userPatrons);
            }
        }
    }

    /**
     * 获取门客ID对应rechargeIdList
     * @return
     */
    private Map<Integer, List<Integer>> getPatronsRechargeListMap() {
        Map<Integer, List<Integer>> map = new HashMap<>();
        String param = GameConfig.ACTIVITY_PEERLESS_NEW_PATRONS_FUND;
        String[] relationStrArr = param.split("\\|");
        for (String relationStr : relationStrArr) {
            String[] arr = relationStr.split("=");
            int patronsId = Integer.parseInt(arr[0]);
            String rechargeIdsStr = arr[1];
            List<Integer> list = new ArrayList<>();
            for (String rechargeIdStr : rechargeIdsStr.split(";")) {
                int rechargeId = Integer.parseInt(rechargeIdStr);
                list.add(rechargeId);
            }
            map.put(patronsId, list);
        }
        return map;
    }

    /**
     * 获取mall rechargeId 和 门客id的映射关系map。k：rechargeId，v：patronsId
     * @return
     */
    private Map<Integer, Integer> getMallRechargeIdPatronsIdMap() {
        Map<Integer, Integer> map = new HashMap<>();
        String param = GameConfig.ACTIVITY_PEERLESS_NEW_PATRONS_FUND;
        String[] relationStrArr = param.split("\\|");
        for (String relationStr : relationStrArr) {
            String[] arr = relationStr.split("=");
            int patronsId = Integer.parseInt(arr[0]);
            String rechargeIdsStr = arr[1];
            for (String rechargeIdStr : rechargeIdsStr.split(";")) {
                map.put(Integer.parseInt(rechargeIdStr), patronsId);
            }
        }
        return map;
    }

    /**
     * 添加门客基金购买记录
     */
    public void addPatronsFundBuyData(MallInfo mallInfo) {
        //根据rechargeId获取门客id
        Map<Integer, Integer> relationMap = this.getMallRechargeIdPatronsIdMap();
        Integer patronsId = relationMap.get(mallInfo.getRechargeId());
        if (patronsId == null) {
            return;
        }
        //新增购买记录
        PatronsFundBuyData patronsFundBuyData = new PatronsFundBuyData();
        patronsFundBuyData.setUserId(player.getUserId());
        patronsFundBuyData.setPatronsId(patronsId);
        patronsFundBuyData.setMallRechargeId(mallInfo.getRechargeId());
        patronsFundBuyData.setCreateTime(System.currentTimeMillis());
        patronsFundBuyData.setInsertOption();
        Map<Integer, PatronsFundBuyData> mallMap = this.patronsFundBuyDataMap.get(patronsId);
        if (mallMap == null) {
            mallMap = new ConcurrentHashMap<>();
            mallMap.put(patronsFundBuyData.getMallRechargeId(), patronsFundBuyData);
            this.patronsFundBuyDataMap.put(patronsId, mallMap);
        } else {
            mallMap.put(patronsFundBuyData.getMallRechargeId(), patronsFundBuyData);
        }
    }

    /**
     * 判断是否购买了该门客基金
     * @param patronsId 门客ID
     * @return
     */
    public boolean checkIsBuyPatronsFund(int patronsId) {
        Map<Integer, PatronsFundBuyData> mallMap = this.patronsFundBuyDataMap.get(patronsId);
        if (mallMap == null) {
            return false;
        }
        return true;
    }

    /**
     * 存储玩家基金信息
     * @param patronsId
     * @param activityId
     * @param patronsQua
     * @param patronsRela
     */
    public void savePatronsFundUserData(int patronsId, int activityId, long patronsQua, long patronsRela) {
        Map<Integer, PatronsFundUser> map = this.patronsFundUserMap.get(activityId);
        if (map == null) {
            map = new ConcurrentHashMap<>();
        }
        PatronsFundUser patronsFundUser = map.get(patronsId);
        if (patronsFundUser == null) {
            patronsFundUser = new PatronsFundUser();
            patronsFundUser.setUserId(player.getUserId());
            patronsFundUser.setActivityId(activityId);
            patronsFundUser.setPatronsId(patronsId);
            patronsFundUser.setPatronsQua(patronsQua);
            patronsFundUser.setPatronsRela(patronsRela);
            patronsFundUser.setCreateTime(System.currentTimeMillis());
            patronsFundUser.setInsertOption();
        } else {
            if (patronsQua > 0) {
                patronsFundUser.setPatronsQua(patronsQua);
            }
            if (patronsRela > 0) {
                patronsFundUser.setPatronsRela(patronsRela);
            }
            patronsFundUser.setUpdateTime(System.currentTimeMillis());
        }
        map.put(patronsFundUser.getPatronsId(), patronsFundUser);
        this.patronsFundUserMap.put(activityId, map);
    }

    /**
     * 存储达成日志
     * @param patronsId
     * @param activityId
     * @param conditionType
     * @param conditionId
     */
    public void savePatronsFundConditionCom(int patronsId, int activityId, int conditionType, int conditionId) {
        Map<Integer, PatronsFundConditionCom> condidtionMap = this.patronsFundConditionComMap.get(activityId);
        if (condidtionMap == null) {
            condidtionMap = new ConcurrentHashMap<>();
        }
        PatronsFundConditionCom patronsFundConditionCom = condidtionMap.get(conditionId);
        if (patronsFundConditionCom == null) {
            PatronsFundConditionCom data = new PatronsFundConditionCom();
            data.setActivityId(activityId);
            data.setUserId(player.getUserId());
            data.setPatronsId(patronsId);
            data.setConditionType(conditionType);
            data.setConditionId(conditionId);
            data.setCreateTime(System.currentTimeMillis());
            data.setInsertOption();
            condidtionMap.put(data.getConditionId(), data);
            this.patronsFundConditionComMap.put(activityId, condidtionMap);
        }
    }
}
